'''
@Version: 0.1.1
@Author: ider
@Date: 2019-12-30 15:49:22
@LastEditors: ider
@LastEditTime: 2020-03-16 10:08:59
@Description: 传输数据
'''


import pika
import pickle
import  pymongo
from util import Sea
import logging
import time
import pickle
from config import SEA_URL, MONGO_URL

Table = pymongo.MongoClient(MONGO_URL).small_world.wikipedia_category_nodes_lv2
SEA = Sea(SEA_URL)
count = 0

def on_message(channel, method_frame, header_frame, body):
    global Table,SEA,count
    doc = pickle.loads(body)
    ret_dict = {
        "_id":doc['_id'],
        "page_id":doc['page_id'],
        'year':doc['year'],
    }
    try:
        logging.debug(f"sea put")
        SEA.put(doc['_id'],'small_world_one_lv2',doc['d_t'])
        logging.debug('mongo insert')
        Table.insert_one(ret_dict)
    except pymongo.errors.DuplicateKeyError:
        pass
    channel.basic_ack(delivery_tag=method_frame.delivery_tag)
    logging.info(count)
    count +=1

def main():
    while 1:
        try:
            credentials = pika.PlainCredentials('shuzhi', 'lambdax')
            pika_connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.1.224',5671,'/',credentials,))

            channel = pika_connection.channel()
            # 声明队列
            channel.queue_declare(queue='small_world_direct_one_lv2')


            #     pika_connection.process_data_events()

            channel.basic_qos(prefetch_count=5)
            channel.basic_consume('small_world_direct_one_lv2', on_message)
            channel.start_consuming()
            logging.info('start consuming')
            time.sleep(9999999)
        except KeyboardInterrupt:
            channel.stop_consuming()
            break

if __name__ == "__main__":
    main()